/*
 *     This file is part of Mobile GPS Logger.
 *
 *     Mobile GPS Logger is free software: you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation, either version 3 of the
 *     License, or (at your option) any later version.
 *
 *     Mobile GPS Logger is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License v3 for more details.
 *
 *     You should have received a copy of the GNU General Public License v3
 *     along with Mobile GPS Logger. If not, see <http://www.gnu.org/licenses/>.
 */

package cx.ath.skyflyer.gpslog.nmea;

/**
 * An object of this class represents a geographic coordinate.
 * @author Ahti Legonkov
 */
public class Coordinate {

    public Coordinate(String c) {
        this(
                c.substring(0, c.length()-1),
                c.substring(c.length()-1, c.length()));
    }

    /**
     *
     * @param latlon latitude or longitude numeric value.
     * @param nesw possible values are N, E, S or W.
     */
    public Coordinate(String latlon, String nesw) {
        double coord = Double.parseDouble(latlon);
        if (coord < 0)
            throw new IllegalArgumentException();

        ilmakaar = nesw;

        degrees = (int)Math.floor(coord/100);
        coord -= 100*degrees;
        minutes = coord;
        wholeMinutes = (int)Math.floor(minutes);
        coord -= wholeMinutes;
        seconds = coord*60;
    }

    public int getDegrees() {
        return degrees;
    }

    public double getMinutes() {
        return minutes;
    }

    public int getWholeMinutes() {
        return wholeMinutes;
    }

    public double getSeconds() {
        return seconds;
    }

    public double getRadians() {

        final double degreeInRadians = Math.PI * 2 / 360.0;
        double res = (double)degrees + minutes / 60.0;
        res *= degreeInRadians;

        if (this.isSouth() || this.isWest())
            return -res;

        return res;
    }

    boolean isNorth() { return ilmakaar.equals("N"); }
    boolean isSouth() { return ilmakaar.equals("S"); }
    boolean isEast() { return ilmakaar.equals("E"); }
    boolean isWest() { return ilmakaar.equals("W"); }

    private int degrees;
    private double minutes;
    private int wholeMinutes;
    private double seconds;

    private String ilmakaar;
}
